home *** CD-ROM | disk | FTP | other *** search
- ########################################################################
- # Module : hmessage.hsm
- # Description: Functions for messages (news, mails) loaded into a list.
- # Maintainer : Juergen Haible <juergen.haible@t-online.de>
- # Version : 2000-07-22
- ########################################################################
- # NOTE:
- # This module is delivered with Hamster and it will be overwritten when
- # installing a new version of it. It might also be used by accompanying
- # demo-scripts, which depend on the current implementation.
- # So do NOT change this file unless you REALLY know what you do!
- ########################################################################
-
- #!load hstrings.hsm
-
- #!initialize
- debug( 255, "<<< module 'hmessage.hsm' >>>" )
- return( 0 )
-
- ########################################################################
- # MsgHeadernameOfIndex: Returns header-name of a given line.
- ########################################################################
- # [IN] $Msg : list containing the message
- # $Idx : index within list pointing to a header-line
- # [OUT] (result): header-name including colon; "" if invalid
- # Example: $HdrNam = MsgHeadernameOfIndex( $Msg, 2 )
-
- sub MsgHeadernameOfIndex( $Msg, $Idx )
- var( $s, $i )
- $s = ListGet( $Msg, $Idx )
- $i = Pos( ":", $s )
- $s = iif( $i>0, copy($s,1,$i), "" )
- return( $s )
- endsub
-
- ########################################################################
- # MsgIndexOfHeader: Returns index of a given header
- ########################################################################
- # [IN] $Msg : list containing the message
- # $HdrNam: name of header including colon
- # [OUT] (result): index, -1 if not found
- # Example: $HdrIdx = MsgIndexOfHeader( $Msg, "From:" )
-
- sub MsgIndexOfHeader( $Msg, $HdrNam )
- var( $i, $Idx )
-
- $Idx = -1
- $i = 0
- $HdrNam = LowerCase( $HdrNam )
-
- while( $i < ListCount($Msg) )
- break( ListGet($Msg,$i) = "" ) # end of header
- if( $HdrNam = LowerCase( MsgHeadernameOfIndex( $Msg, $i ) ) )
- $Idx = $i
- break
- endif
-
- inc( $i )
- endwhile
-
- return( $Idx )
-
- endsub
-
- ########################################################################
- # MsgGetHeader: Returns value of a given header
- ########################################################################
- # [IN] $Msg : list containing the message
- # $HdrNam: name of header including colon
- # [OUT] (result): value of header, "" if not found
- # Example: $HdrVal = MsgGetHeader( $Msg, "From:" )
-
- sub MsgGetHeader( $Msg, $HdrNam )
- var( $HdrVal, $Idx, $i, $s )
-
- # find position of header
- $Idx = MsgIndexOfHeader( $Msg, $HdrNam )
-
- # if found, return value of header
- if( $Idx >= 0 )
- $HdrVal = ListGet( $Msg, $Idx )
- $i = Pos( ":", $HdrVal )
- $HdrVal = iif( $i=0, "", copy($HdrVal,$i+1) )
- $s = copy( $HdrVal, 1, 1 )
- if( Pos($s,$WSP) > 0 )
- $HdrVal = Delete( $HdrVal, 1, 1 )
- endif
-
- # append continuation lines separated by CR+LF
- do
- inc( $Idx )
- $s = copy( ListGet( $Msg, $Idx ), 1, 1 )
- break( Pos($s,$WSP)=0 ) # 1st char=WSP?
- $HdrVal = $HdrVal + $CRLF + ListGet( $Msg, $Idx )
- loop
- else
- $HdrVal = ""
- endif
-
- # return value of header (i.e. not including header-name)
- return( $HdrVal )
-
- endsub
-
- ########################################################################
- # MsgDelHeader: Deletes a given header-line.
- ########################################################################
- # [IN] $Msg : list containing the message
- # $HdrNam: name of header including colon
- # [OUT] (result): former index of deleted line, -1 if not found
- # Example: $OldIdx = MsgDelHeader( $Msg, "X-Posting-Agent:" )
-
- sub MsgDelHeader( $Msg, $HdrNam )
- var( $HdrVal, $Idx, $i, $s )
-
- # find position of header
- $Idx = MsgIndexOfHeader( $Msg, $HdrNam )
-
- # if found, delete it and its continuation lines
- if( $Idx >= 0 )
- ListDelete( $Msg, $Idx )
- do
- $s = copy( ListGet( $Msg, $Idx ), 1, 1 )
- break( Pos($s,$WSP)=0 ) # 1st char=WSP?
- ListDelete( $Msg, $Idx )
- loop
- endif
-
- # return old position or -1 for 'not found'
- return( $Idx )
-
- endsub
-
- ########################################################################
- # MsgAddHeader: Adds a header-line, EVEN if message already contains one
- # with the given name (see also: MsgSetHeader)
- ########################################################################
- # [IN] $Msg : list containing the message
- # $HdrNam: name of header including colon
- # $HdrVal: value of header
- # [OUT] (result): index of new header-line, -1 on error
- # Example: $AddIdx = MsgAddHeader( $Msg, "X-I-Like-X-Headers:", "No" )
-
- sub MsgAddHeader( $Msg, $HdrNam, $HdrVal )
- var( $i, $Idx )
-
- # find header/body-separator
- $Idx = ListIndexOf( $Msg, "" )
-
- # if missing, add header/body-separator
- if( $Idx < 0 )
- ListInsert( $Msg, 0, "" )
- $Idx = 0
- endif
-
- # add header-line right before header/body-separator
- ListInsert( $Msg, $Idx, $HdrNam + " " + $HdrVal )
-
- # return postion of new header-line
- return( $Idx )
-
- endsub
-
- ########################################################################
- # MsgSetHeader: Adds a header-line. If message already contains one with
- # the given name, it will be replaced with the new one.
- ########################################################################
- # [IN] $Msg : list containing the message
- # $HdrNam: name of header including colon
- # $HdrVal: value of header
- # [OUT] (result): index of new/updated header-line, -1 on error
- # Example: $AddIdx = MsgSetHeader( $Msg, "X-I-Like-X-Headers:", "No" )
-
- sub MsgSetHeader( $Msg, $HdrNam, $HdrVal )
- var( $Idx )
-
- # delete existing header-line; save old position
- $Idx = MsgDelHeader( $Msg, $HdrNam )
-
- # add new header-line
- if( $Idx>=0 )
- # insert at old position
- ListInsert( $Msg, $Idx, $HdrNam + " " + $HdrVal )
- else
- # append to headers
- $Idx = MsgAddHeader( $Msg, $HdrNam, $HdrVal )
- endif
-
- # return postion of new/changed header-line
- return( $Idx )
-
- endsub
-
- ########################################################################
-